/**
 * 
 */
package de.mbenning.weather.wunderground.console;

import java.text.SimpleDateFormat;
import java.util.List;

import de.mbenning.weather.wunderground.api.domain.DataSet;
import de.mbenning.weather.wunderground.api.domain.HttpProxy;
import de.mbenning.weather.wunderground.api.domain.WeatherStation;
import de.mbenning.weather.wunderground.api.domain.WeatherStations;
import de.mbenning.weather.wunderground.impl.services.HttpDataReaderService;
import de.mbenning.weather.wunderground.impl.services.WeatherStationService;

/**
 * @author Martin.Benning
 *
 */
public class WundergroundConsole {
	
	public final static void main(String[] args) throws InterruptedException {
		WeatherStationService weatherStationService = new WeatherStationService();
		weatherStationService.setHttpProxy(new HttpProxy());
		
		HttpDataReaderService httpDataReaderService = new HttpDataReaderService();
		httpDataReaderService.setHttpProxy(new HttpProxy());
		
		System.out.println("\n==================================================================================");
		System.out.println("\t\t\tWUNDERGROUND-CONSOLE-APP");
		System.out.println("\t\t\t2011(C) BY MARTIN BENNING");
		System.out.println("==================================================================================\n");		
		
		if(args.length == 0) {
			System.out.println("nothing to do - no arguments given ... ");
			return;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
		
		boolean isLoop = false;
		boolean isFullFormat = false;
		boolean isMediumFormat = false;
		long sleep = 0;
		boolean isMaxDew = false;
		boolean isMinDew = false;
		boolean isMaxTemp = false;
		boolean isMinTemp = false;
		boolean isListStations = false;
		boolean isCountry = false;
		String listCountry = null;
		
		do {
			for(String arg : args) {
				if(arg == null) return;
				
				if(args != null && args.length > 0 && args[0].equalsIgnoreCase("--help")) {
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("Weatherstations - IDs:");
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("Boxmeer = INOORDBR35");
					System.out.println("Dortmund = INORDRHE72");
					System.out.println("Goes = IZEELAND13");
					System.out.println("Coevorden = IDRENTHE48");
					System.out.println("Kleve = INRWKLEV2");
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("Header-Descriptions:");
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("DATETIME = timestamp of dataset");
					System.out.println("TEMP     = Temperature in Celsius");
					System.out.println("DEW      = Dew-Point in Celsius");
					System.out.println("WSPD     = Windspeed in km/h");
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("Options:");
					System.out.println("----------------------------------------------------------------------------------");
					System.out.println("--help		show this view");
					System.out.println("--noproxy	data request not through proxy server");
					System.out.println("--medium	display medium columns data line");
					System.out.println("--full		display full columns data line");
					System.out.println("--loop		fetching permanent data from station in a loop");
					System.out.println("--pause[x]  	if loop is set wait for [x] millis between next fetch cycle start");
					System.out.println("--maxdew	shows the dataset with maximum dewpoint value");
					System.out.println("--mindew	shows the dataset with minimum dewpoint value");
					System.out.println("--maxtemp	shows the dataset with maximum temperature value");
					System.out.println("--mintemp	shows the dataset with minimum temperature value");
					System.out.println("----------------------------------------------------------------------------------");
					return;
				} 
				
				if(arg.equalsIgnoreCase("--noproxy")) {
					httpDataReaderService.getHttpProxy().setEnabled(false);
					weatherStationService.getHttpProxy().setEnabled(false);
					continue;
				}
				if(arg.equalsIgnoreCase("--loop")) {
					isLoop = true;
					continue;
				}
				if(arg.startsWith("--pause") && isLoop) {
					String[] millis = arg.split("e");
					sleep = Long.parseLong(millis[1]);
					continue;
				}
				if(arg.equalsIgnoreCase("--full")) {
					isFullFormat = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--medium")) {
					isMediumFormat = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--maxdew")) {
					isMaxDew = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--mindew")) {
					isMinDew = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--maxtemp")) {
					isMaxTemp = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--mintemp")) {
					isMinTemp = true;
					continue;
				}
				if(arg.equalsIgnoreCase("--list")) {
					isListStations = true;
					continue;
				}
				if(isListStations) {
					listCountry = (arg != null) ? arg : listCountry; 
					List<WeatherStation> stations = weatherStationService.findAllWeatherStationsByCountry(listCountry);
					for (WeatherStation weatherStation : stations) {
						System.out.println(weatherStation.getStationId() + "\t" + (weatherStation.getNeighborhood()!=null ? weatherStation.getNeighborhood() : "") + "\t" + weatherStation.getCity() + "\t" + weatherStation.getCountry());
					}
					break;
				}
				if(arg.equalsIgnoreCase("--country")) {
					isCountry = true;
					continue;
				}
				if(isCountry) {
					listCountry = (arg != null) ? arg : listCountry; 
					isCountry = false;
					continue;
				}
				
				String stationId = (arg != null || arg.equalsIgnoreCase("")) ? arg : "INOORDBR35";
				WeatherStation weatherStation = WeatherStations.ALL.get(stationId);
				if(weatherStation == null && listCountry != null) {
					weatherStation = weatherStationService.getWeatherStation(listCountry, stationId);
				}
				
				if(weatherStation == null) {
					weatherStation = new WeatherStation(stationId);
				}
				
				httpDataReaderService.setWeatherStation(weatherStation);
				DataSet current = null;
				if(isMinDew) {
					current = httpDataReaderService.minDewPoint();
				} else if(isMaxDew) {
					current = httpDataReaderService.maxDewPoint();
				} else if(isMinTemp) {
					current = httpDataReaderService.minTemperature();
				} else if(isMaxTemp) {
					current = httpDataReaderService.maxTemperature();
				} else {
					current = httpDataReaderService.getCurrentData();
				}
				
				if(current == null) {
					System.out.println("[ERROR]: could not evaluate weather dataset ...");
					continue;
				}
				
				String city 	= weatherStation.getCity() != null ? weatherStation.getCity() : "";
				String state 	= weatherStation.getState() != null ? weatherStation.getState() : "";
				String country 	= weatherStation.getCountry() != null ? weatherStation.getCountry() : "";
				
				System.out.println(weatherStation.getStationId() + " " + city + " " + state + " " + country);
				if(isFullFormat) {
					System.out.println("DATETIME\t TEMP\tDEW\tWSPD");
					System.out.println(sdf.format(current.getDateTime()) + " " + current.getTemperature() + "\t" + current.getDewPoint() + "\t" + current.getWindSpeedKmh());
				} else if(isMediumFormat){
					System.out.println("DATETIME\t TEMP\tDEW\tWSPD");
					System.out.println(sdf.format(current.getDateTime()) + " " + current.getTemperature() + "\t" + current.getDewPoint() + "\t" + current.getWindSpeedKmh());
				} else {
					System.out.println("DATETIME\t TEMP");
					System.out.println(sdf.format(current.getDateTime()) + " " + current.getTemperature());
				}
				System.out.println("");
			}
			
			if(isLoop) {
				System.out.println("-----------------------------------------\n");
				Thread.currentThread().sleep(sleep);
			}
		} while(isLoop);
	}
}
